﻿using Common.Encrypt;
using EWPlatAPP.FModels;
using EWPlatAPP.UControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXIMEW.BLL;
using ZXIMEW.Models.DModels;

namespace EWPlatAPP
{
    public partial class FrmLogin : FormParent
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            InitInfo();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void InitInfo()
        {
            txtUName.Clear();
            txtUPwd.Clear();
            lblNameErr.Visible = false;
            lblPwdErr.Visible = false;
            lblLoginErr.Visible = false;
        }

        /// <summary>
        /// 登录实现
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            lblNameErr.Visible = false;
            lblPwdErr.Visible = false;
            lblLoginErr.Visible = false;
            //接收信息
            string name = txtUName.Text.Trim();
            string pwd = txtUPwd.Text.Trim();
            //检查信息
            if (string.IsNullOrEmpty(name))
            {
                lblNameErr.Visible = true;
                txtUName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(pwd))
            {
                lblPwdErr.Visible = true;
                txtUPwd.Focus();
                return;
            }
            //登录过程：数据库里检查
            //要把账号密码拿数据表中进行检查--是否存在
            string enPwd = MD5Encrypt.Encrypt(pwd);
            UserInfo userInfo = new UserInfo()
            {
                UserName = name,
                UserPwd = enPwd
            };
            //到数据库检查
            UserBLL userBLL = new UserBLL();
            UserInfo reUser = userBLL.Login(userInfo);
            if (reUser != null)
            {
                if (reUser.UserState)
                {
                    this.Hide();
                    FrmMain frmMain = new FrmMain();
                    frmMain.Tag = new FMainData()
                    {
                        LoginData = reUser,
                        FLogin = this
                    };
                    frmMain.Show();
                }
                else
                {
                    lblLoginErr.Visible = true;
                    lblLoginErr.Text = "该用户已冻结,不能使用系统";
                }
            }
            else
            {
                lblLoginErr.Visible = true;
                lblLoginErr.Text = "用户名账户或密码有误,请检查!";
            }
        }
    }
}
